<template>
    <div class="boxs">
        <el-card shadow="always" :body-style="{ padding: '20px' }">
            <div slot="header">
                <span>员工批量导入上传</span>
            </div>
            <UploadExcel :on-success="handleSuccess" :before-upload="beforeUpload"></UploadExcel>
        </el-card>
        

    </div>
</template>

<script>
// 导入接口
import {importEmployeesAPI} from '@/api/employees'
// 导入工具方法
import {transformKeys} from '@/utils';
    export default {
        methods: {
            // 校验本地文件的excel的大小
    beforeUpload(file) {
      const isLt1M = file.size / 1024 / 1024 < 1

      if (isLt1M) {
        // 校验通过=》上传的大小《1m
        return true
      }

      this.$message({
        message: 'Please do not upload files larger than 1m in size.',
        type: 'warning'
      })
        // 校验不通过
      return false
    },
   async handleSuccess({ results, header }) {
      console.log('enen',results, header);
      const maps = {
  '入职日期': 'timeOfEntry',
  '聘用形式':'formOfEmployment', 
  '手机号': 'mobile',
  '姓名': 'username',
  '转正日期': 'correctionTime',
  '工号': 'workNumber',
  '部门':'departmentName'
}
// ['timeOfEntry','correctionTime'] 指定需要格式化的数据
      const data = transformKeys(results,maps,['timeOfEntry','correctionTime'])
      console.log(data);
      await importEmployeesAPI(data)
    // 跳回员工管理页面
    this.$router.push('/employees')
    }
  }
        
    }
</script>

<style lang="sass" scoped>

</style>